function formatDate(time: Date, format: string) {
  const year = time.getFullYear()
  const month = time.getMonth() + 1
  const date = time.getDate()
  const weekDay = time.getDay().toFixed()
  const hours = time.getHours()
  const minutes = time.getMinutes()
  const seconds = time.getSeconds()
  const wholeMonth = fillZero(month)
  const wholeDate = fillZero(date)
  const wholeHours = fillZero(hours)
  const wholeMinutes = fillZero(minutes)
  const wholeSeconds = fillZero(seconds)

  const symbol = format.slice(4, 5)
  const tempDay = year + symbol + wholeMonth + symbol + wholeDate
  const tempTime = wholeHours + ":" + wholeMinutes + ":" + wholeSeconds
  if (["YYYY-MM-DD", "YYYY/MM/DD"].includes(format.split(" ")[0])) {
    if (format.split(" ").length === 1) {
      return tempDay
    } else if (format.split(" ").length === 2) {
      return tempDay + " " + tempTime
    }
  } else if (format === "weekDay") {
    let templateDays = {
      "0": "星期日",
      "1": "星期一",
      "2": "星期二",
      "3": "星期三",
      "4": "星期四",
      "5": "星期五",
      "6": "星期六",
    }
    return templateDays[weekDay]
  }
}

function fillZero(time: number) {
  return time.toFixed().length === 1 ? "0" + time : time
}

export default formatDate
